package com.mxz.common.core.sensitive;

import java.util.function.Function;

/**
 * 相关字段脱敏处理
 * 字段上加
 * @Sensitive(strategy = SensitiveStrategy.PHONE)
 */
public enum SensitiveStrategy {
    /**
     * 用户姓名脱敏策略
     */
    FULL_NAME(s -> s.length()>2?s.replaceAll("(\\S)\\S*(\\S)", "$1*$2"):s.replaceAll("(\\S)\\S*", "$1*")),
    /**
     * 身份证脱敏策略
     */
    ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")),
    /**
     * 电话脱敏策略
     */
    PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
    /**
     * 地址脱敏策略
     */
    ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****")),
    /**
     * 银行卡脱敏策略
     */
    BANK_CARD(s -> s.replaceAll("\\d+(?=\\d{4})", "**** ")),
    /**
     * 其他字段脱敏
     */
    OTHER(s -> "*");

    private final Function<String, String> desensitizer;

    SensitiveStrategy(Function<String, String> desensitizer) {
        this.desensitizer = desensitizer;
    }

    public Function<String, String> desensitizer() {
        return desensitizer;
    }
}